//
//  EvetnSearchViewController.m
//  Runone
//
//  Created by runone02 on 16/9/1.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "EvetnSearchViewController.h"
#import "UIView+Addition.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "RequestGinsengModel.h"
#import "RequestHelper.h"
#import "ReportEvent.h"
#import "TollEvent.h"
#import "EventDetailModel.h"
#import "CJCalendarViewController.h"
#import "CurrentEvent.h"
#import "SVProgressHUD.h"
#import "PromptUtil.h"
#import "UIView+Message.h"
#import "MJExtension.h"
#import "TollEvent.h"
#import "ReportEvent.h"
#import "AppConstants.h"
#import "CurrentContructDetailViewController.h"
#import "CurrentIncidentDetailViewController.h"
#import "HistoryInidentDetailController.h"
#import "HistoryConstructDetailController.h"
#import "TollStationEventDetailController.h"
#import "EventBaseCell.h"
#import "AppLocalManager.h"
#import "EventTypeTableViewController.h"
#import "BDMTollEventTypeDefineInfoModel.h"
#import "UnStartEventController.h"
#import "InprogressController.h"

#define   kScreenWidth   [UIScreen mainScreen].bounds.size.width
#define   PageSize       10
@interface EvetnSearchViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,CalendarViewControllerDelegate, UIPopoverPresentationControllerDelegate>
@property (strong, nonatomic) UISearchBar *searchBar;
@property (strong, nonatomic) NSString *textString;
@property (strong, nonatomic) UITableView *tableView;
@property (assign, nonatomic) NSInteger currentPageIndex;
@property (strong, nonatomic) NSMutableArray *searchDataArray;
@property (copy, nonatomic) NSString *lastEventUID;
@property (assign, nonatomic) NSString *searchWay;
@property (copy, nonatomic) NSString *searchContent;
@property (nonatomic,strong)PromptUtil *popMessage;
@property (nonatomic,strong)UIView *meesageV;
@property (nonatomic, strong)UIButton *calendarBtn;
@property (nonatomic, strong) UIButton *constructEventBtn; //施工事件
@property (nonatomic, strong) UIButton *incidentEventBtn;  //事故事件
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *verLineView;
@property (nonatomic, strong) UIButton  *tollStationBtn;

@property (nonatomic, copy) NSString *tollStationStr;
@property (nonatomic, assign) BOOL constructOrIncident; //事故还是施工
@property (nonatomic, assign) NSInteger pageInt;
@property (nonatomic, assign) BOOL generalID;
@end

@implementation EvetnSearchViewController

static NSString *cellID = @"searchCell";
#pragma mark - laZy


- (void)viewDidLoad {
    [super viewDidLoad];
    _pageInt = 1;
    self.view.backgroundColor = kViewBackGroundColor;
    _searchDataArray = [[NSMutableArray alloc] init];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.currentPageIndex = 1;
    self.searchWay = @"1";
    //设置UI
    [self configNavitationBar];
    [self configCalendarBtn];
    [self configEventTypeView];
    [self configTableView];
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(serachNewData)];
    self.tableView.mj_footer.hidden = YES;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(searchMoreData)];
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
    [_searchBar resignFirstResponder];
    
}
- (void)configNavitationBar {
    _searchBar = [[UISearchBar alloc] init];
    _searchBar.placeholder = @"请输入搜索的关键字";
    self.navigationItem.titleView = _searchBar;
    _searchBar.delegate = self;
    [_searchBar becomeFirstResponder];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left_narrow"] style:UIBarButtonItemStylePlain target:self action:@selector(popBack)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(confirmSearch)];
}

- (void) searchMoreData {
    if (_generalID) {
        [self searchDataFromDataOrWord];
    }else {
        if (_containerView) {
            if (_constructOrIncident) {
                [self searchConstructMoreData];
                
            }else {
                [self searchIncidentMoreData];
            }
        } else {
            [self searchTollMoreData];
        }
    }
}

- (void)searchDataFromDataOrWord {
    self.currentPageIndex += 1;
    [_searchBar resignFirstResponder];
    if (self.titleType == 0) {
        CurrentEvent *ceModel = [self.searchDataArray lastObject];
        self.lastEventUID = ceModel.IncidentUID;
    }
    
    if (self.titleType == 1) {
        CurrentEvent *ceModel = [self.searchDataArray lastObject];
        self.lastEventUID = ceModel.IncidentUID;
    }
    
    if (self.titleType == 2) {
        TollEvent *tollModel = [self.searchDataArray lastObject];
        self.lastEventUID = tollModel.EventUID;
    }
    
    if (self.titleType == 3)  {
        CurrentEvent *reportModel = [self.searchDataArray lastObject];
        self.lastEventUID = reportModel.IncidentUID;
    }
    NSString *searchContent ;
    if ([self.searchWay isEqualToString:@"1"]) {
        searchContent = _searchBar.text;
    }else {
        searchContent = self.searchContent;
    }
    NSInteger eventTag = _titleType ;
    NSString *eventClass = [NSString stringWithFormat:@"%ld",(long)eventTag];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
    SearchEventModel.URL         = GetEventDataURL;
    SearchEventModel.DataValue   = [self DataValueStr:self.lastEventUID
                                            pageIndex:[NSString stringWithFormat:@"%ld",(long)self.currentPageIndex] pageSize:@"10" eventClass:eventClass searchWay:_searchWay searchContent:searchContent];;
    
    SearchEventModel.UpdatedTime = @"";
    SearchEventModel.SystemCode  = @"GuangleHighway";
    SearchEventModel.MethodName  = @"SearchEvent";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            if (self.titleType == 0 || self.titleType == 1 ) {
                NSArray *array = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
                [self.searchDataArray addObjectsFromArray:array];
            } else if (self.titleType == 2) {
                NSArray *array = [TollEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
                [self.searchDataArray addObjectsFromArray:array];
            }else {
                NSArray *array = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
                [self.searchDataArray addObjectsFromArray:array];
            }
            if (_searchDataArray.count == 0) {
                [SVProgressHUD showSuccessWithStatus:@"没有查询到相关事件"];
            }
            if ([requestOutginseng.Code isEqualToString:@"001"]&& _searchDataArray.count >0) {
                [self.view addSubview:_tableView];
                
                [_tableView reloadData];
                [_searchBar resignFirstResponder];
                
            }
            [self.tableView.mj_footer endRefreshing];
        }else {
            self.currentPageIndex -= 1;
        }
    };
    [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
}

- (void) searchConstructMoreData {
    CurrentEvent *model = [_searchDataArray lastObject];
    _pageInt += 1;
    _titleType = 1;
    _constructOrIncident = YES;
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
    SearchEventModel.URL         = GetEventDataURL;
    SearchEventModel.DataValue   = [self DataValueStr:model.EventUID pageIndex:[NSString stringWithFormat:@"%ld",(long)_pageInt] pageSize:@"10" incidentType:@"3"];
    SearchEventModel.UpdatedTime = @"";
    SearchEventModel.SystemCode  = @"GuangleHighway";
    SearchEventModel.MethodName  = @"SearchEventByType";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        
        if ([requestOutginseng.Code isEqualToString:@"001"] ) {
            [self.view addSubview:_tableView];
            NSArray *array = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            [_searchDataArray addObjectsFromArray:array];
            [_tableView reloadData];
            self.tableView.mj_footer.hidden = YES;
        }else {
            _pageInt -= 1;
        }
    };
    [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
}

- (void) searchIncidentMoreData {
    CurrentEvent *model = [_searchDataArray lastObject];
    _pageInt += 1;
    _titleType = 1;
    _constructOrIncident = NO;
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
    SearchEventModel.URL         = GetEventDataURL;
    SearchEventModel.DataValue   = [self DataValueStr:model.EventUID pageIndex:@"0" pageSize:@"10" incidentType:@"6"];
    SearchEventModel.UpdatedTime = @"";
    SearchEventModel.SystemCode  = @"GuangleHighway";
    SearchEventModel.MethodName  = @"SearchEventByType";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        
        if ([requestOutginseng.Code isEqualToString:@"001"] ) {
            [self.view addSubview:_tableView];
            NSArray *array = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            [_searchDataArray addObjectsFromArray:array];
            [_tableView reloadData];
            self.tableView.mj_footer.hidden = YES;
        }else {
            _pageInt -= 1;
        }
    };
    [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
}

- (void) searchTollMoreData {
    TollEvent *model = [_searchDataArray lastObject];
    _pageInt += 1;
    if (![_tollStationBtn.titleLabel.text isEqualToString:@"请选择收费事件类型"]) {
        RequestHelper *requestHelper=[[RequestHelper alloc] init];
        RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
        SearchEventModel.URL         = GetEventDataURL;
        SearchEventModel.DataValue   = [self DataValueStr:model.EventUID pageIndex:[NSString stringWithFormat:@"%ld",(long)_pageInt] pageSize:@"10" tollEventType:self.tollStationStr];
        SearchEventModel.UpdatedTime = @"";
        SearchEventModel.SystemCode  = @"GuangleHighway";
        SearchEventModel.MethodName  = @"SearchTollEventByType";
        requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
            
            //            [_searchDataArray removeAllObjects];
            if ([requestOutginseng.Code isEqualToString:@"001"] ) {
                [self.view addSubview:_tableView];
                NSArray *array = [TollEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
                [_searchDataArray addObjectsFromArray:array];
                [_tableView reloadData];
                self.tableView.mj_footer.hidden = YES;
            }else {
                _pageInt -= 1;
            }
        };
        [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
    }
    
}

- (void) configEventTypeView {
    if (_titleType == 2) {
        _tollStationBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 74, [UIScreen mainScreen].bounds.size.width - 40, 30)];
        [_tollStationBtn setTitle:@"请选择收费事件类型" forState:UIControlStateNormal];
        [_tollStationBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _tollStationBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_tollStationBtn setBackgroundColor:[UIColor whiteColor]];
        [_tollStationBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_tollStationBtn addTarget:self action:@selector(tollStationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_tollStationBtn];
    }else if (_titleType == 1) {
        _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, 44)];
        _containerView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_containerView];
        
        _verLineView = [[UIView alloc] initWithFrame:CGRectMake(self.view.width / 2, 2, 1, 40)];
        _verLineView.backgroundColor = [UIColor lightGrayColor];
        [_containerView addSubview:_verLineView];
        
        _constructEventBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, (self.view.width / 2), 44)];
        [_constructEventBtn setTitle:@"施工事件" forState:UIControlStateNormal];
        [_constructEventBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _constructEventBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_constructEventBtn addTarget:self action:@selector(constructEventBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_containerView addSubview: _constructEventBtn];
        
        _incidentEventBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.width) / 2, 0, (self.view.width / 2), 44)];
        [_incidentEventBtn setTitle:@"事故事件" forState:UIControlStateNormal];
        _incidentEventBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_incidentEventBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_incidentEventBtn addTarget:self action:@selector(incidentEventBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_containerView addSubview:_incidentEventBtn];
    }
}
#pragma mark - 搜索施工事件
-(NSString *)DataValueStr:(NSString *)lastEventUID pageIndex:(NSString *)pageIndex pageSize:(NSString *)pageSize incidentType:(NSString *)incidentType {
    NSString *valueStr =[NSString stringWithFormat:@"[{\"Key\":\"LastEventUID\",\"Value\":\"%@\"},{\"Key\":\"PageIndex\",\"Value\":\"%@\"},{\"Key\":\"PageSize\",\"Value\":\"%@\"},{\"Key\":\"IncidentType\",\"Value\":\"%@\"}]",lastEventUID,pageIndex,pageSize,incidentType];
    NSLog(@"%@",valueStr);
    return valueStr;
}
-(NSString *)DataValueStr:(NSString *)lastEventUID pageIndex:(NSString *)pageIndex pageSize:(NSString *)pageSize tollEventType:(NSString *)tollEventType {
    NSString *valueStr =[NSString stringWithFormat:@"[{\"Key\":\"LastEventUID\",\"Value\":\"%@\"},{\"Key\":\"PageIndex\",\"Value\":\"%@\"},{\"Key\":\"PageSize\",\"Value\":\"%@\"},{\"Key\":\"TollEventType\",\"Value\":\"%@\"}]",lastEventUID,pageIndex,pageSize,tollEventType];
    NSLog(@"%@",valueStr);
    return valueStr;
}

- (void) tollStationBtnClick:(UIButton *)btn {
    __weak typeof(self) weakSelf = self;
    AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    EventTypeTableViewController *eventVC=[[EventTypeTableViewController alloc] init];
    eventVC.modalPresentationStyle = UIModalPresentationPopover;
    eventVC.LineBlock=^(NSInteger index, NSString *eventTypeStr){
        //        eventType = [NSString stringWithFormat:@"%ld",index];
        NSInteger inter = index - 1 ;
        BDMTollEventTypeDefineInfoModel *model  = myDelegate.allTollEvent[inter];
        weakSelf.tollStationStr = [NSString stringWithFormat:@"%lu",(unsigned long)model.TollEventType];
        [weakSelf.tollStationBtn setTitle:model.TollEventName forState:UIControlStateNormal];
        [weakSelf requestTollEvent];
    };
    eventVC.popoverPresentationController.sourceView   = btn;
    eventVC.popoverPresentationController.delegate     = self;
    eventVC.popoverPresentationController.sourceRect   = CGRectMake(130, 138, 0, 0);
    eventVC.popoverPresentationController.permittedArrowDirections = NO;
    
    [self presentViewController:eventVC animated:YES completion:nil];
    _titleType = 2;
    
}
//_DataValue	__NSCFString *	@"[{\"Key\":\"LastEventUID\",\"Value\":\"\"},{\"Key\":\"PageIndex\",\"Value\":\"0\"},{\"Key\":\"PageSize\",\"Value\":\"10\"},{\"Key\":\"TollEventType\",\"Value\":\"2\"}]"	0x000000017012e600
- (void) requestTollEvent {
    if (![_tollStationBtn.titleLabel.text isEqualToString:@"请选择收费事件类型"]) {
        RequestHelper *requestHelper=[[RequestHelper alloc] init];
        RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
        SearchEventModel.URL         = GetEventDataURL;
        SearchEventModel.DataValue   = [self DataValueStr:@"" pageIndex:@"0" pageSize:@"10" tollEventType:self.tollStationStr];
        SearchEventModel.UpdatedTime = @"";
        SearchEventModel.SystemCode  = @"GuangleHighway";
        SearchEventModel.MethodName  = @"SearchTollEventByType";
        requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
            
            [_searchDataArray removeAllObjects];
            if ([requestOutginseng.Code isEqualToString:@"001"] ) {
                [self.view addSubview:_tableView];
                _searchDataArray = [TollEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
                [_tableView reloadData];
            }
        };
        [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
    }
}



- (void) constructEventBtnClick:(UIButton *)btn {
    _titleType = 1;
    _constructOrIncident = YES;
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_incidentEventBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
    SearchEventModel.URL         = GetEventDataURL;
    SearchEventModel.DataValue   = [self DataValueStr:@"" pageIndex:@"0" pageSize:@"10" incidentType:@"3"];
    SearchEventModel.UpdatedTime = @"";
    SearchEventModel.SystemCode  = @"GuangleHighway";
    SearchEventModel.MethodName  = @"SearchEventByType";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        
        [_searchDataArray removeAllObjects];
        if ([requestOutginseng.Code isEqualToString:@"001"] ) {
            [self.view addSubview:_tableView];
            _searchDataArray = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            [_tableView reloadData];
        }
    };
    [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
}

#pragma mark - 事故事件
- (void) incidentEventBtnClick:(UIButton *)btn {
    _titleType = 1;
    _constructOrIncident = NO;
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_constructEventBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
    SearchEventModel.URL         = GetEventDataURL;
    SearchEventModel.DataValue   = [self DataValueStr:@"" pageIndex:@"0" pageSize:@"10" incidentType:@"6"];
    SearchEventModel.UpdatedTime = @"";
    SearchEventModel.SystemCode  = @"GuangleHighway";
    SearchEventModel.MethodName  = @"SearchEventByType";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        
        [_searchDataArray removeAllObjects];
        if ([requestOutginseng.Code isEqualToString:@"001"] ) {
            [self.view addSubview:_tableView];
            _searchDataArray = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            [_tableView reloadData];
        }
    };
    [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
}

- (void) confirmSearch {
    if (self.searchBar.text.length >1) {
        self.searchWay = @"2";
        self.currentPageIndex = 1;
        _generalID = YES;
        [self searchDataFromServer];
    }
}
- (void)navigationBackClick {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) calendarBtnClick {
    CJCalendarViewController *calendarController = [[CJCalendarViewController alloc] init];
    calendarController.view.frame = self.view.frame;
    calendarController.delegate   = self;
    [self presentViewController:calendarController animated:YES completion:nil];
}
#pragma mark - 日历控件的代理
-(void)CalendarViewController:(CJCalendarViewController *)controller didSelectActionYear:(NSString *)year month:(NSString *)month day:(NSString *)day{
    
    [_searchBar resignFirstResponder];
    self.searchWay = @"2";
    self.searchContent = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
    self.currentPageIndex = 1;
    _generalID =YES;
    [self searchDataFromServer];
}
#pragma mark - <UISearchBarDelegate>
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    
    if (searchBar.text) {
        [_searchDataArray removeAllObjects];
        [_tableView reloadData];
    }
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    //    [self.view addSubview:_tableView];
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
    if([searchBar.text length] > 0) {  // 有输入
        self.searchWay = @"1";
        _generalID = YES;
        [self searchDataFromServer];//请求服务器
    }else { // 无输入
        return;
    }
}
- (void)popBack {
    [self navigationBackClick];
}
#pragma mark - configUI
- (void)configTableView {
    CGFloat y = 64;
    if (self.titleType == 0 || self.titleType == 3) { //当前事件 和 施工事件
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, y, self.view.bounds.size.width, self.view.bounds.size.height - y)];
    }else {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, y + 44, self.view.bounds.size.width, self.view.bounds.size.height - y - 44)];
    }
    
    //    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 108, self.view.bounds.size.width, self.view.bounds.size.height - 108)];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView registerNib:[UINib nibWithNibName:@"EventBaseCell" bundle:nil] forCellReuseIdentifier:cellID];
}
- (void)configCalendarBtn {
    _calendarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _calendarBtn.backgroundColor    = [UIColor lightGrayColor];
    [_calendarBtn setBackgroundImage:[UIImage imageNamed:@"btn_calendar"] forState:UIControlStateNormal];
    _calendarBtn.frame              = CGRectMake(self.view.bounds.size.width / 2 - 30, 118, 60, 60);
    _calendarBtn.layer.cornerRadius = 29;
    _calendarBtn.backgroundColor    = [UIColor whiteColor];
    _calendarBtn.clipsToBounds      = YES;
    [_calendarBtn addTarget:self action:@selector(calendarBtnClick)
           forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_calendarBtn];
}
#pragma mark - 发送

- (void)searchDataFromServer {
    self.pageInt += 1;
    [_searchBar resignFirstResponder];
    if (self.titleType == 0) {
        CurrentEvent *ceModel = [self.searchDataArray lastObject];
        self.lastEventUID = ceModel.IncidentUID;
    }
    
    if (self.titleType == 1) {
        CurrentEvent *ceModel = [self.searchDataArray lastObject];
        self.lastEventUID = ceModel.IncidentUID;
    }
    
    if (self.titleType == 2) {
        TollEvent *tollModel = [self.searchDataArray lastObject];
        self.lastEventUID = tollModel.EventUID;
    }
    
    if (self.titleType == 3)  {
        CurrentEvent *reportModel = [self.searchDataArray lastObject];
        self.lastEventUID = reportModel.IncidentUID;
    }
    NSString *searchContent ;
    if ([self.searchWay isEqualToString:@"1"]) {
        searchContent = _searchBar.text;
    }else {
        searchContent = self.searchContent;
    }
    NSInteger eventTag = _titleType + 1;
    NSString *eventClass = [NSString stringWithFormat:@"%ld",(long)eventTag];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *SearchEventModel=[[RequestGinsengModel alloc] init];
    SearchEventModel.URL         = GetEventDataURL;
    SearchEventModel.DataValue   = [self DataValueStr:@"" pageIndex:[NSString stringWithFormat:@"%ld",(long)self.currentPageIndex] pageSize:@"10" eventClass:eventClass searchWay:_searchWay searchContent:searchContent];;
    SearchEventModel.UpdatedTime = @"";
    SearchEventModel.SystemCode  = @"GuangleHighway";
    SearchEventModel.MethodName  = @"SearchEvent";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            [_searchDataArray removeAllObjects];
            
            if (self.titleType == 0 || self.titleType == 1 ) {
                self.searchDataArray = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            } else if (self.titleType == 2) {
                self.searchDataArray = [TollEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            }else {
                self.searchDataArray = [CurrentEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            }
            if (_searchDataArray.count == 0) {
                [SVProgressHUD showSuccessWithStatus:@"没有查询到相关事件"];
            }
            if ([requestOutginseng.Code isEqualToString:@"001"]&& _searchDataArray.count >0) {
                [self.view addSubview:_tableView];
                
                [_tableView reloadData];
                [_searchBar resignFirstResponder];
            }
        }
    };
    [requestHelper RequestGinsengModel:SearchEventModel andPushController:self];
}
#pragma mark - dataValueString
-(NSString *)DataValueStr:(NSString *)lastEventUID pageIndex:(NSString *)pageIndex pageSize:(NSString *)pageSize eventClass:(NSString *)eventClass searchWay:(NSString *)searchWay searchContent:(NSString *)searchContent {
    NSString *valueStr =[NSString stringWithFormat:@"[{\"Key\":\"LastEventUID\",\"Value\":\"%@\"},{\"Key\":\"PageIndex\",\"Value\":\"%@\"},{\"Key\":\"PageSize\",\"Value\":\"%@\"},{\"Key\":\"EventClass\",\"Value\":\"%@\"},{\"Key\":\"SearchWay\",\"Value\":\"%@\"},{\"Key\":\"SearchContent\",\"Value\":\"%@\"}]",lastEventUID,pageIndex,pageSize,eventClass,searchWay,searchContent];
    NSLog(@"%@",valueStr);
    return valueStr;
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EventBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (_titleType == 0 || _titleType== 1 || _titleType == 3){ // 当前事件 历史事件 施工事件
        
        cell.backgroundColor = kViewBackGroundColor;
        CurrentEvent *model = self.searchDataArray[indexPath.row];
        
        if (_titleType == 3){
            cell.markInt = EventListTitleTypeConstruct;
        }
        if (_titleType == 1) {
            cell.markInt = EventListTitleTypeHistory;
        }
        if (_titleType == 0) {
            cell.markInt = EventListTitleTypeCurrent;
        }
        
        cell.ceModel = model;
    }
    
    
    if (_titleType == 2) {  //收费事件
        TollEvent *model = self.searchDataArray[indexPath.row];
        cell.tollData = model;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(_titleType == 0 || _titleType == 3) {
        CurrentEvent *model = [_searchDataArray objectAtIndex:indexPath.row];
        CGFloat cellH;
        CGFloat height;
        if ((model.DealDetail)) {
            NSString *tempStr = model.DealDetail;
            
            cellH = [self caculateCellHeight:tempStr];
            int colun = cellH / 16; // 校准
            if (colun == 1) {
                height = 110;
            }else {
                height = cellH + 16 + 44 + 5 + 20 + 8 + colun * 0.293;
            }
            
        }else {
            height = 110;
        }
        return height;
        
    }else if (_titleType == 1) {
        CurrentEvent *model = [_searchDataArray objectAtIndex:indexPath.row];
        CGFloat cellH;
        CGFloat height;
        if ((model.Content)) {
            NSString *tempStr = model.Content;
            
            cellH = [self caculateCellHeight:tempStr];
            int colun = cellH / 16; // 校准
            if (colun == 1) {
                height = 110;
            }else {
                height = cellH + 16 + 44 + 5 + 20 + 8 + colun * 0.293;
            }
            
        }else {
            height = 110;
        }
        return height;
        
    }
    else {
        return 110;
    }
}

#pragma mark - 计算cell的高度
- (CGFloat) caculateCellHeight:(NSString *)string  {
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGSize maxSize   = CGSizeMake([UIScreen mainScreen].bounds.size.width - 68, MAXFLOAT);
    CGFloat dealHeight = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:attributes context:nil].size.height;
    return dealHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_titleType == 0) { //当前事件
        CurrentEvent *model = _searchDataArray[indexPath.row];
        if ([model.IncidentParentType integerValue] == 3) {  //施工事件
            CurrentContructDetailViewController *vc= [[CurrentContructDetailViewController alloc] init];
            vc.CEModel = model;
            [self.navigationController pushViewController:vc animated:YES];
        } else {  //事故事件
            CurrentIncidentDetailViewController *vc = [[CurrentIncidentDetailViewController alloc] init];
            vc.CEModel = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
    if (_titleType == 1) { //历史事件
        CurrentEvent *model = _searchDataArray[indexPath.row];
        if ([model.IncidentParentType integerValue] == 3) {  //施工事件
            HistoryConstructDetailController *vc = [[HistoryConstructDetailController alloc] init];
            vc.CEModel = model;
            [self.navigationController pushViewController:vc animated:YES];
        } else {  //事故事件
            HistoryInidentDetailController *vc = [[HistoryInidentDetailController alloc] init];
            vc.CEModel = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
    if (_titleType == 2) { //收费事件
        TollStationEventDetailController *vc = [[TollStationEventDetailController alloc] init];
        vc.model = self.searchDataArray[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (_titleType == 3) { //施工事件
        CurrentEvent *model = _searchDataArray[indexPath.row];
        NSString *str     = [[AppLocalManager sharedManager] getDataByKey:SYSUSERUID];
        if ([model.RecordUserUID isEqualToString:str]) {
            if (model.State == 1) { // 未开始
                UnStartEventController *vc = [[UnStartEventController alloc] init];
                vc.reportModel              = model;
                [self.navigationController pushViewController:vc animated:YES];
            }
            if (model.State == 2) { // 进行中
                InprogressController *vc = [[InprogressController alloc]init];
                vc.reportModel               = model;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } else {
            [SVProgressHUD showSuccessWithStatus:@"不能编辑他人事件"];
            return;
        }
    }
}
@end
